#include "common.h"
#include "list.h"

List* g_pList=NULL;
//创建顺序表，固定长度
List* CreateList() {
    g_pList = (List*)malloc(sizeof(List));
    if (g_pList == NULL) {
        return NULL;
    }
    memset(g_pList->data, 0, sizeof(dev_fd) * SIZE);
    g_pList->iCount = 0;
    return g_pList;
}
//销毁顺序表
void DestroyList(List* pList) {
    if (pList != NULL) {
        free(pList);
        pList = NULL;
    }
}
//删除顺序表中指定位置的元素
int DeleteList(List* pList, dev_fd* pData, int iOffset) {
    if (pList == NULL || iOffset < 0||iOffset >= pList->iCount) {
        return SYS_ERROR;
    }
    *pData = pList->data[iOffset];
    int i;
    for (i = iOffset; i < pList->iCount-1; i++) {
        pList->data[i] = pList->data[i + 1];
    }
    pList->iCount--;
    return SYS_OK;
}
//向顺序表中插入数据
int InsertList(List* pList, dev_fd tData, int iOffset) {
    if (pList == NULL || iOffset < 0 ||  pList->iCount >= SIZE) {
        return SYS_ERROR;
    }
    int i;
    for (i = pList->iCount - 1; i >= iOffset; i--) {
        pList->data[i + 1] = pList->data[i];
    }
    pList->iCount++;
    pList->data[iOffset].fd = tData.fd;
    pList->data[iOffset].lTime = tData.lTime;
    return SYS_OK;
}
//修改元素-在顺序表中用tNew替换tOld
int UpdateList(List* pList, dev_fd tNew, dev_fd tOld) {
    if (pList == NULL) {
        return SYS_ERROR;
    }
    int i;
    for (i = 0; i < pList->iCount; i++) {
        if (pList->data[i].fd == tOld.fd&&pList->data[i].lTime==tOld.lTime) {
            pList->data[i] = tNew;
            return SYS_OK;
        }
    }
    return SYS_ERROR;
}
//在顺序表中查找指定的元素，并返回下标，失败返回SYS_ERROR
int SearchItem(List* pList, dev_fd  pData) {
    int i;
    if (pList == NULL) {
        return SYS_ERROR;
    }
    for (i = 0; i < pList->iCount; i++) {
        if (pList->data[i].fd == pData.fd && pList->data[i].lTime == pData.lTime) {
            return i;
        }
    }
    return SYS_ERROR;
}
//显示顺序表中元素
void ShowList(List* pList) {
    int i;
    if (pList == NULL) {
        return;
    }
    for (i = 0; i < pList->iCount; i++) {
        printf("%3d:fd: %d, lTime: %ld\n", i,pList->data[i].fd, pList->data[i].lTime);
    }
}
//删除在顺序表中超时的设备信息并关闭文件描述符，并返回是否需要修改select监听范围
int DeleteTimeOutDevFromList(List* pList, fd_set* pSet, int iMax)
{
    if (pList == NULL || pSet == NULL)
    {
        return SYS_ERROR;
    }
    int needUpdate = 0;
    int newMax = iMax;
    int i;
    dev_fd* pData;
    for (i = 0; i < pList->iCount; )
    {
        if (pList->data[i].lTime > iMax)
        {
            // 关闭文件描述符
            close(pList->data[i].fd);
            // 从fd_set中移除
            FD_CLR(pList->data[i].fd, pSet);
            // 从顺序表中删除
            if (DeleteList(pList, pData,i) == SYS_ERROR)
            {
                return SYS_ERROR;
            }
            // 更新是否需要修改select监听范围的标志
            needUpdate = 1;
            // 更新新的最大描述符
            for (int j = 0; j < pList->iCount; j++)
            {
                if (pList->data[j].fd > newMax)
                {
                    newMax = pList->data[j].fd;
                }
            }
        }
        else
        {
            i++;
        }
    }
    if (needUpdate)
    {
        return SYS_UPDATE_MAX_FD;
    }
    return SYS_OK;
}
